'use strict';
function pack() {
    const a = require('glob'), b = require('fs'), c = require('path'), e = require('crypto'), zlib = require('zlib'), uglifyjs = require('uglify-js'), babel = require('babel-core');
    module.exports = async (f, destPath, h = {}) => {
        var i = [new Buffer(1), new Buffer(4), new Buffer(4), new Buffer(4), new Buffer(1)];
        i[0].writeIntLE(190), i[1].writeInt32BE(1), i[4].writeIntLE(237);
        let j = 0, k = [], l = [], m = [], n = [], o = [];
        return new Promise((resolve, reject) => {
            let r = Object.assign({nodir: !0}, h), fileMd5Info = {}, totalSize = 0;
            a(`${f}/**`, r, (a, r) => {
                if (!a) {
                    r.forEach((a) => {
                        let d = b.readFileSync(a), g = c.relative(f, a);

                        // 处理js
                        if (/\.js$/.test(g)) {
                            var maxSize = 1024 * 500; // 过滤大于500K的JS文件
                            var fileInfo = b.statSync(a);
                            if (fileInfo.size > maxSize) return;
                            let result = babel.transform(b.readFileSync(a), {
                                presets: ['es2015', 'stage-0'],
                                babelrc: false,
                                filename: g,
                                sourceFileName: g,
                                comments: false,
                                //minified: true,
                                sourceMaps: 'map',
                            });
                            let code = result.code, map = result.map
                            delete map.__proto__;
                            map = JSON.stringify(map);
                            d = new Buffer(uglifyjs.minify(code).code, 'utf8'); // 重置
                            b.writeFileSync(a + ".map", map)
                            r.push(a + ".map");
                        }

                        if (h.needMd5) {
                            let a = e.createHash('md5');
                            a.update(d);
                            let b = a.digest('hex');
                            if (fileMd5Info[g] = b, h.ignoreFileMd5 && h.ignoreFileMd5[g] == b) return
                        }
                        let i = new Buffer(`/${g.replace(/\\/g, '/')}`);
                        j++, m.push(i), n.push(d), /\.js\.map$/.test(a) || (totalSize += d.length, totalSize += i.length, totalSize += 12)
                    });
                    let a = 18 + 12 * j + Buffer.concat(m).length;
                    l = m.map((b, c) => {
                        let d = new Buffer(4);
                        d.writeInt32BE(b.length);
                        let e = new Buffer(4), f = n[c].length, g = a;
                        e.writeInt32BE(g), a += f;
                        let h = new Buffer(4);
                        return h.writeInt32BE(f), Buffer.concat([d, b, e, h])
                    });
                    let q1 = new Buffer(4);
                    q1.writeInt32BE(j), l.unshift(q1), k = Buffer.concat(l), o = Buffer.concat(n), i[2].writeInt32BE(k.length), i[3].writeInt32BE(o.length), i = Buffer.concat(i);
                    let data = Buffer.concat([i, k, o]);
                    let gzipData = zlib.gzipSync(data);
                    //b.writeFileSync(destPath, gzipData)
                    totalSize += 18
                    resolve({
                        destPath: destPath,
                        data: gzipData,
                        totalSize: totalSize,
                        fileMd5Info: fileMd5Info
                    })
                } else reject(a)
            })
        })
    }
}
pack()